package xiaolong.leetcode.medium;

import java.util.Iterator;
import java.util.LinkedList;

public class add_two_nums {
	public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
		ListNode result, node;
		ListNode p = l1;
		ListNode q = l2;
		result = new ListNode(0) ;
		node = result;
		int x, y, sum, carry = 0;
		while (q != null || p != null) {
			if (q != null) {
				x = q.val;
				q = q.next;
			} else {
				x = 0;
			}
			if (p != null) {
				y = p.val;
				p = p.next;
			} else {
				y = 0;
			}
			sum = x + y + carry;
			node.next = new ListNode(sum % 10);
			node = node.next;
			carry = sum / 10;
		}
		if (carry == 1) {
			node.next = new ListNode(1);
			node = node.next;
		}
		return result.next;
	}

	public static void main(String[] args) {
		ListNode s1 = new ListNode(0);
		ListNode s2 = new ListNode(0);
		ListNode p = s1;
		ListNode q = s2;
		p.next = new ListNode(2);
		q.next = new ListNode(3);
		// q = q.next;
		// q = new ListNode(3);
		// q = q.next;
		// q = new ListNode(3);
		// q = q.next;
		// q = new ListNode(3);
		// q = q.next;
		// q = new ListNode(3);
		add_two_nums a = new add_two_nums();
		ListNode b = a.addTwoNumbers(s1, s2);
		System.out.println(b);
	}
}
